// pages/brand_centent/index.js
const WxParse = require('../../wxParse/wxParse.js')
import {
    getNavigationbar
} from "../../api/index.js"
Page({
    options: {
        styleIsolation: "shared"
    },
    /**
     * 页面的初始数据
     */
    data: {
        logistic: '',
        details: {},
        title: '',content:'',height:'100',width:'100'
    },
   
    formatRichText(html, p_class = "fsmp") {
        if (!html) {
            return ''
        }

        let newContent = html
        if (html.indexOf("<img") != -1) {
            newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
                match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "")
                match = match.replace(/style=""/gi, "")
                match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "")
                match = match.replace(/height="[^"]+"/gi, "").replace(/height='[^']+'/gi, "")
                return match
            })
            newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
                match = match.replace(/width:[^;]+;/gi, "max-width:100%;").replace(/width:[^;]+;/gi, "max-width:100%;")
                return match
            })
            newContent = newContent.replace(/<br[^>]*\/>/gi, "")
            newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;padding-bottom: 20px;"')
        }

        newContent = newContent.replace(/<br[^>]*\/>/gi, "")
        newContent = newContent.replace(/<a>/gi, `<a class="${p_class} p_class_color "`)
        newContent = newContent.replace(/<li>/gi, `<li class="${p_class} p_class_color "`)
        newContent = newContent.replace(/\<p/gi, `<p class="${p_class} p_class_color "`)
        newContent = newContent.replace(/\<span/gi, `<span class="${p_class} p_class_color "`)

        return newContent
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log('options.data',options)
        // if (options.data) {
        //     let params = JSON.parse(options.data)
        //     console.log('options.data',params)
            // this.data.details = {
            //     ...params
            // }
            // console.log('进', params, this.data.details)
            // var that = this;
            getNavigationbar({
                act: 'agent_acticity',
                agentid: options.agentid || '',
            }).then(res => { 
                var that = this;
                console.log('re',res)
                
                wx.nextTick(() => { 
                    var article = res.content
                    WxParse.wxParse('article', 'html', article, that, 5);
                }) 
                const richText = res.content;
                const formattedRichText = this.formatRichText(richText, "brand-introduce");
                this.setData({
                    title: res.title,
                    // content:formattedRichText,
    
                })
               
            })
          

        // }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})